/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inter;

import Logic.Conectividad;
import Logic.DtMP;
import Logic.MessageBox;
import Logic.This;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ronaldo
 */
public class CtrCompra extends javax.swing.JInternalFrame {

    public int Status = 0;

    /**
     * Creates new form CtrCompra
     */
    public CtrCompra() {
        initComponents();
        Clean();
        Recalc();
    }

    private String FechaFull() {
        Calendar calendario = new GregorianCalendar();
        return String.valueOf(calendario.get(Calendar.DAY_OF_MONTH))
                + "/" + String.valueOf(calendario.get(Calendar.MONTH) + 1)
                + "/" + String.valueOf(calendario.get(Calendar.YEAR));
    }

    private void Clean() {
        txtCodigo.setText(This.AlfaNum(10));
        txtFecha.setText(FechaFull());
        javax.swing.table.DefaultTableModel Modelo = (javax.swing.table.DefaultTableModel) jTable1.getModel();
        Modelo.setRowCount(0);
        cantidad.setValue(1);
        costo.setText("0");
        Recalc();
    }
    //Control de Codigos de Proveedor
    ArrayList CodigoProveedor = new ArrayList();

    public void LoadProveedor() {
        try {
            javax.swing.DefaultComboBoxModel Modelo = (javax.swing.DefaultComboBoxModel) cbxProveedor.getModel();
            Modelo.removeAllElements();
            String Sql = "SELECT COD_PROV,NOMBRE FROM PROVEEDOR";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Result = Cons.executeQuery(Sql);
            CodigoProveedor.clear();
            int i = 0;
            while (Result.next()) {
                CodigoProveedor.add(i, Result.getString(1));
                Modelo.addElement(Result.getString(2));
                i++;
            }

            if (Modelo.getSize() <= 0) {
                setVisible(false);
                Status = 1;
                MessageBox.show(this, "Sin proveedor registrado", "Lo sentimos, pero no se ha registrado proveedor alguno\n"
                        + "Favor de Registrar Proveedor", MessageBox.Error);
            } else {
                Status = 0;
            }
            //txtDescProveedor.setText(CodigoProveedor.get(cbxProveedor.getSelectedIndex()).toString());
        } catch (SQLException ex) {
            Logger.getLogger(CtrMPrima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ArrayList CodMP = new ArrayList();

    private void LoadMPrima() {
        try {
            String Codigo = CodigoProveedor.get(cbxProveedor.getSelectedIndex()).toString();
            javax.swing.DefaultComboBoxModel Modelo = (javax.swing.DefaultComboBoxModel) cbxMPrima.getModel();
            Modelo.removeAllElements();
            //WHERE COD_PV='" + Codigo.trim() + "'
            String Sql = "SELECT COD_MP,NOMBRE FROM MPRIMA WHERE COD_PV='" + Codigo.trim() + "'";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Result = Cons.executeQuery(Sql);
            int i = 0;
            while (Result.next()) {
                CodMP.add(i, Result.getString(1));
                Modelo.addElement(Result.getString(2));
                i++;
            }
            cbxMPrima.setModel(Modelo);
        } catch (SQLException ex) {
            //Logger.getLogger(CtrCompra.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            //Logger.getLogger(CtrCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void LoadDMP() {
//        try {
//            String Sql = "SELECT EXISTENCIA,COSTO FROM MPRIMA WHERE COD_MP='" + CodMP.get(this.cbxMPrima.getSelectedIndex()) + "'";
//            System.out.println(Sql);
//            java.sql.Statement Cons = Conectividad.Conector.createStatement();
//            java.sql.ResultSet Result = Cons.executeQuery(Sql);
//            Result.next();
//            int Max = Result.getInt(1);
//            float Costo=Result.getFloat(2);
//            cantidad.setModel(new javax.swing.SpinnerNumberModel(0, 0, Max, 1));
//            costo.setText(String.valueOf(Costo));
//        } catch (SQLException ex) {
//            //Logger.getLogger(CtrCompra.class.getName()).log(Level.SEVERE, null, ex);
//        } catch(Exception ex){
//            
//        }
    }

    private void AddMP() {
        try {
            String CMP = (String) CodMP.get(this.cbxMPrima.getSelectedIndex());

            if (Verify(CMP)) {
                return;
            }
            if (Float.parseFloat(cantidad.getValue().toString()) <= 0) {
                MessageBox.show(this, "Error", "Favor seleccione la cantidad a comprar", MessageBox.Error);
                return;
            }

            if (Float.parseFloat(costo.getText()) <= 0) {
                MessageBox.show(this, "Error", "Favor, Ingrese el costo de compra", MessageBox.Error);
                return;
            }


            javax.swing.table.DefaultTableModel Modelo = (javax.swing.table.DefaultTableModel) jTable1.getModel();
            java.util.Vector v = new java.util.Vector();
            v.add(CMP);
            v.add(cbxMPrima.getSelectedItem().toString());
            v.add(cantidad.getValue());
            v.add(costo.getText());
            v.add(Float.parseFloat(cantidad.getValue().toString()) * Float.parseFloat(costo.getText()));
            Modelo.addRow(v);
            cbxProveedor.setEnabled(false);
        } catch (Exception e) {
            MessageBox.show(this, "Error", "Verifica tus datos", MessageBox.Error);
        }
    }

    private void Recalc() {
        float STotal = 0, Iva = 0, Total = 0;

        for (int i = 0; i < jTable1.getRowCount(); i++) {
            STotal += Float.parseFloat(jTable1.getValueAt(i, 4).toString());
        }
        Iva = (float) (STotal * 0.15);
        Total = (float) (STotal * 1.15);
        txtSTotal.setText(String.valueOf(STotal));
        txtIva.setText(String.valueOf(Iva));
        txtTotal.setText(String.valueOf(Total));
    }

    private boolean Verify(String Codigo) {
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            if (jTable1.getValueAt(i, 0).toString().equals(Codigo)) {
                return true;
            }
        }
        return false;
    }

    private void Save() {
        try {
            String CCompra=this.txtCodigo.getText();
            String CProvee = CodigoProveedor.get(cbxProveedor.getSelectedIndex()).toString();
            java.sql.Statement Cons=Conectividad.Conector.createStatement();
            String Sql="INSERT INTO COMPRA VALUES('"+CCompra+"','"+CProvee+"',NOW(),0,23)";
            Cons.executeUpdate(Sql);
            for(int i=0;i<jTable1.getRowCount();i++){
                Sql="INSERT INTO DETAIL_COMPRA VALUES(null,'"+CCompra+"','"+jTable1.getValueAt(i, 0).toString() +"',"+jTable1.getValueAt(i, 2).toString() +","+jTable1.getValueAt(i, 3).toString() +")";
                Cons.executeUpdate(Sql);
                Sql="UPDATE MPRIMA SET EXISTENCIA=EXISTENCIA+"+jTable1.getValueAt(i, 2).toString() +",COSTO="+jTable1.getValueAt(i, 3).toString() +" WHERE COD_MP='"+jTable1.getValueAt(i, 0).toString() +"'";
                Cons.executeUpdate(Sql);
            }
            MessageBox.show(this,"Registrado Exitosamente!","Se ha registrado la compra exitosamente!",MessageBox.Informacion);
            Clean();
        } catch (SQLException ex) {
            Logger.getLogger(CtrCompra.class.getName()).log(Level.SEVERE, null, ex);
            MessageBox.show(this,"Error","Se ha presentado un Error, imposible registrar",MessageBox.Error);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbxProveedor = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cbxMPrima = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cantidad = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        costo = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        txtSTotal = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtIva = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        lblSave = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Control de Compras de Materia Prima");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Control de Compras", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información Principal", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel1.setText("Codigo Compra");

        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel2.setText("Proveedor");

        cbxProveedor.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        cbxProveedor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxProveedorItemStateChanged(evt);
            }
        });
        cbxProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxProveedorActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel3.setText("Fecha");

        txtFecha.setEditable(false);
        txtFecha.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(241, 241, 241)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(cbxProveedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbxProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detalle de Información", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jPanel4.setBackground(new java.awt.Color(7, 101, 152));
        jPanel4.setPreferredSize(new java.awt.Dimension(0, 3));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 3, Short.MAX_VALUE)
        );

        jLabel4.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel4.setText("Materia Prima");

        cbxMPrima.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        cbxMPrima.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxMPrimaItemStateChanged(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel5.setText("Cantidad");

        cantidad.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        cantidad.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(1.0f), null, Float.valueOf(1.0f)));

        jLabel6.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel6.setText("Costo");

        costo.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        costo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                costoKeyTyped(evt);
            }
        });

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/list-add.png"))); // NOI18N
        jLabel7.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/list-remove.png"))); // NOI18N
        jLabel8.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Materia Prima", "Cantidad", "Costo", "SutTotal"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 846, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(cbxMPrima, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(costo, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbxMPrima, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(costo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(4, 4, 4)))
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Finalizando", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jLabel9.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel9.setText("SutTotal");

        txtSTotal.setEditable(false);
        txtSTotal.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel10.setText("Iva - 15%");

        txtIva.setEditable(false);
        txtIva.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N

        jLabel11.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jLabel11.setText("Total");

        txtTotal.setEditable(false);
        txtTotal.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N

        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png"))); // NOI18N
        lblSave.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblSaveMouseClick1(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblSaveMouseExit1(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblSaveMouseEnter1(evt);
            }
        });

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit-clear.png"))); // NOI18N
        jLabel12.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel12MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtSTotal)
                    .addComponent(txtTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSave)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtSTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(lblSave)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 168, Short.MAX_VALUE))
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblSaveMouseClick1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseClick1
        // TODO add your handling code here:
        Save();
    }//GEN-LAST:event_lblSaveMouseClick1

    private void lblSaveMouseExit1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseExit1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png")));
    }//GEN-LAST:event_lblSaveMouseExit1

    private void lblSaveMouseEnter1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseEnter1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile2.png")));
    }//GEN-LAST:event_lblSaveMouseEnter1

    private void jLabel12MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel12MouseClicked
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Seguro¿?", "Seguro¿?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            Clean();
        }
    }//GEN-LAST:event_jLabel12MouseClicked

    private void cbxProveedorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxProveedorItemStateChanged
        // TODO add your handling code here:
        LoadMPrima();
    }//GEN-LAST:event_cbxProveedorItemStateChanged

    private void cbxProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxProveedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxProveedorActionPerformed

    private void cbxMPrimaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxMPrimaItemStateChanged
        // TODO add your handling code here:
        LoadDMP();
    }//GEN-LAST:event_cbxMPrimaItemStateChanged

    private void costoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_costoKeyTyped
        // TODO add your handling code here:
        This.ValidNumber(evt);
    }//GEN-LAST:event_costoKeyTyped

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        // TODO add your handling code here:
        try {
            javax.swing.table.DefaultTableModel Modelo = (javax.swing.table.DefaultTableModel) jTable1.getModel();
            Modelo.removeRow(jTable1.getSelectedRow());
            if (Modelo.getRowCount() <= 0) {
                cbxProveedor.setEnabled(true);
            }
        } catch (Exception e) {
        }
        Recalc();
    }//GEN-LAST:event_jLabel8MouseClicked

    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        // TODO add your handling code here:
        AddMP();
        Recalc();
    }//GEN-LAST:event_jLabel7MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner cantidad;
    private javax.swing.JComboBox cbxMPrima;
    private javax.swing.JComboBox cbxProveedor;
    private javax.swing.JTextField costo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblSave;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtFecha;
    private javax.swing.JTextField txtIva;
    private javax.swing.JTextField txtSTotal;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}
